//
//  MainMenuViewController.m
//  Politics
//
//  Created by Hai Nguyen on 7/5/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MainMenuViewController.h"
#import "HelpViewController.h"
#import "StatisticViewController.h"
#import "SettingViewController.h"
#import "HomeGameViewController.h"
#import "AppDelegate_iPhone.h"
#import "QuestionModel.h"
#import "FBUtilities.h"
#import "FBPoster.h"

@implementation MainMenuViewController
@synthesize viewDelegate;
@synthesize newGameButton;
@synthesize helpButton;
@synthesize statisButton;
@synthesize settingButton;
@synthesize logoutButton;
@synthesize shareFBButton;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[self.view addSubview:loadingView];
	[loadingView setFrame:CGRectMake(0, 0, 320, 480)];
	[loadingView setHidden:YES];
    [super viewDidLoad];
	indexAnimation = 0;
	newGameButton.center = CGPointMake(-120,newGameButton.center.y);
	helpButton.center = CGPointMake(-120,helpButton.center.y);
	statisButton.center = CGPointMake(480,statisButton.center.y);
	settingButton.center = CGPointMake(480,settingButton.center.y);
	logoutButton.center = CGPointMake(-120,logoutButton.center.y);
}
- (void)didEndGetGameData:(NSMutableArray*)gameArray
{
	NSLog(@"Num of question %d",[gameArray count]);
	mainiPhoneAppDelegate.numOfCorrect =0;
	mainiPhoneAppDelegate.latestQuestionIndex = 0;
	[mainiPhoneAppDelegate.arrayGames setArray:gameArray];
	if ([mainiPhoneAppDelegate.arrayGames count]!=0) 
	{
		[Audio playStartRound];
		[loadingView setHidden:YES];
		HomeGameViewController *controller = [[HomeGameViewController alloc] init];
		[self.navigationController pushViewController:controller animated:YES];
		[controller release];
	}
	else {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Can not load the game at this time, try gain!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
		[alert show];
		[alert release];
		[loadingView setHidden:YES];
	}

}
- (IBAction)buttonPressed:(id) sender
{
	[Audio playButtonClicked];
	UIButton *button  =  (UIButton*) sender;
	switch (button.tag)
	{
		case 0:
		{	
			//new game
			GameDataParser *gameDataParser = [[GameDataParser alloc] init];
			[gameDataParser getGameDataWithDelegate:self];
			[loadingView setHidden:NO];
		}
			break;
		case 1:
		{	
			// statictis
			StatisticViewController *statisticController = [[StatisticViewController alloc] initWithNibName:@"StatisticViewController" bundle:nil];
			[self.navigationController pushViewController:statisticController animated:YES];
			[statisticController release];
		}
			break;
		case 2:
		{	
			// help
			HelpViewController *helpController = [[HelpViewController alloc] initWithNibName:@"HelpViewController" bundle:nil];
			[self.navigationController pushViewController:helpController animated:YES];
			[helpController release];
		}
			break;
		case 3:
		{	
			// setting
			SettingViewController *settingController = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];
			[self.navigationController pushViewController:settingController animated:YES];
			[settingController release];
		}
			break;
		case 4:
		{	
			// logout
			NSLog(@"Pressed Log out");
			[viewDelegate logout];
		}
			break;
		case 5:
		{		
			// SHARE facebook
			{
				FBSession *fbSession = [FBSession session];
				if (fbSession == nil)
				{
					[mainiPhoneAppDelegate.FButilities facebookLogin:self];
				}
				else if(fbSession.isConnected)
				{
					[FBPoster PostToWall:@"Today, I have played the Politics Game. It's so fun!"];
				}
				else
				{
					[mainiPhoneAppDelegate.FButilities facebookLogin:self];
				}
			}
		}
		break;
		default:
			break;
	}
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	self.newGameButton = nil;
	self.helpButton = nil;
	self.statisButton = nil;
	self.settingButton = nil;
	self.logoutButton = nil;
	self.shareFBButton=nil;
    [super dealloc];
}

- (void)viewDidAppear:(BOOL)animated {
	NSLog(@"view did appear in main menu");
	[self appearanceAnimation];
}

- (void)appearanceAnimation {
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.3];
	newGameButton.center = CGPointMake(160,newGameButton.center.y);
	helpButton.center = CGPointMake(160,helpButton.center.y);
	statisButton.center = CGPointMake(160,statisButton.center.y);
	settingButton.center = CGPointMake(160,settingButton.center.y);
	logoutButton.center = CGPointMake(160,logoutButton.center.y);
	[UIView commitAnimations];
}


@end
